<?php


namespace app\service;


class SaiYouCloud
{

    protected $base_url;
    protected $timestamp;
    protected $signType;

    public function __construct()
    {
        $this->signType = 'normal';
        $this->base_url = 'http://api.mysubmail.com/';
        $this->timestamp = $this->getTimestamp();
    }

    /**
     * 获取时间戳
     * @return mixed
     */
    public function getTimestamp()
    {
        $url = $this->base_url . 'service/timestamp.json';
        $res = curlRequest($url);
        $res = json_decode($res, true);
        return $res['timestamp'];
    }

    /**
     * @param $request
     * @return mixed
     */
    public function subMail($request)
    {
        $api = $this->base_url . 'voice/send.json';
        $request['appid'] = env('syc.app_id');
        $request['timestamp'] = $this->getTimestamp();
        $request['sign_type'] = $this->signType;
        $request['signature'] = $this->createSignature($request);
        return $this->APIHttpRequestCURL($api, $request);
    }

    protected function createSignature($request)
    {
        $r = "";
        switch ($this->signType) {
            case 'normal':
                $r = env('syc.app_key');
                break;
            case 'md5':
            case 'sha1':
                $r = $this->buildSignature($this->argSort($request));
                break;
        }
        return $r;
    }

    protected function buildSignature($request)
    {
        $arg = "";
        $app = env('syc.app_id');
        $app_key = env('syc.app_key');
        foreach ($request as $key => $val) {
            $arg .= $key . "=" . $val . "&";
        }
        $arg = substr($arg, 0, strlen($arg) - 2);
        if (get_magic_quotes_gpc()) {
            $arg = stripslashes($arg);
        }

        if ($this->signType == 'sha1') {
            $r = sha1($app . $app_key . $arg . $app . $app_key);
        } else {
            $r = md5($app . $app_key . $arg . $app . $app_key);
        }

        return $r;
    }

    protected function argSort($request)
    {
        ksort($request);
        reset($request);
        return $request;
    }

    protected function APIHttpRequestCURL($api, $post_data, $method = 'post')
    {
        if ($method != 'get') {
            $ch = curl_init();
            curl_setopt_array($ch, array(
                CURLOPT_URL => $api,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => http_build_query($post_data),
                CURLOPT_CUSTOMREQUEST => strtoupper($method),
                CURLOPT_HTTPHEADER => array("Content-Type: application/x-www-form-urlencoded")
            ));
        } else {
            $url = $api . '?' . http_build_query($post_data);
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        $output = trim($output, "\xEF\xBB\xBF");
        return json_decode($output, true);
    }
}